package com.asia5b.a5mall.middleware.logic.viewmodel.goods

import android.app.Activity
import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.goods.Goods2SkuModel
import com.asia5b.a5mall.middleware.model.bean.ShareBean
import com.asia5b.a5mall.middleware.utils.ThreadFromUtils
import com.asia5b.a5mall.middleware.utils.weak
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.showMessage
import com.asia5b.a5bmall.umeng.logic.UmengShare
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import io.reactivex.Observable
import io.reactivex.functions.Consumer
import io.reactivex.functions.Function3
import java.io.Serializable

/**
 * Created by zhangyuncai on 2020/1/21.
 */
class GoodsDetail2ActivityVM :SimpleViewModel() {

    val loadDataLiveData=MutableLiveData<Array<*>>()

    /**
     * @Note   加载数据
     * @param  goodsId 商品ID
     */
    fun loadData(goodsId: Int) {

        /*todo 商品详情包括收藏状态,店铺详情*/
        val goodsDetail = goodsApi.getGoodsDetail(goodsId)
                .flatMap { goods2Model ->
                    if (!UserRepository.isLogin()) {
                        return@flatMap Observable.just(goods2Model)
                    }
                    /*todo 店铺收藏状态*/
                    return@flatMap goodsApi.getCollectionState(goodsId)
                            .map { shopCollectModel ->
                                goods2Model.absGoodsCollect = shopCollectModel.message
                                return@map goods2Model
                            }
                }
                .flatMap { goods2Model ->
                    /*todo 店铺数据*/
                    return@flatMap shopApi.getShop(goods2Model.absGoodsShopId)
                            .map {
                                goods2Model.shopModel = it
                                return@map goods2Model
                            }
                }
                .flatMap { goods2Model ->
                    /*todo 优惠券*/
                    return@flatMap promotionApi.getSellerCoupon(goods2Model.absGoodsShopId)
                            .map {
                                goods2Model.couponList = it
                                return@map goods2Model
                            }
                }
                .flatMap { goods2Model ->
                    /*todo 活动*/
                    val promotion = promotionApi.getPromotion(goods2Model.absGoodsId)
                    return@flatMap promotion
                            .map {
                                goods2Model.promotionList = it
                                return@map goods2Model
                            }
                }
        /*todo 商品sku*/
        val sku = goodsApi.getGoodsSku(goodsId)
                .map {
                    return@map it
                }
        /*todo 商品评论*/
        val comment = goodsApi.listComment(goodsId, null, null, 1, 10)
                .map { resultModel ->
                    return@map resultModel
                }

        val observable = Observable.zip(goodsDetail,
                sku, comment,
                Function3<AbsGoodsModel, ArrayList<Goods2SkuModel>, ResultModel<*>, Array<Serializable>>
                { goodsDetail, sku, commentResultModel ->
                    var array = arrayOf(goodsDetail, sku, commentResultModel)
                    return@Function3 array
                })

        baseSubscribe(observable, Consumer { array ->
//            providerView().loadDataResult(array[0] as AbsGoodsModel, array[1] as ArrayList<Goods2SkuModel>, array[2] as ResultModel<*>)
            loadDataLiveData.value=array
        })


    }


    val collectGoodsLiveData=MutableLiveData<Boolean>()
    /**
     * @Note   关注收藏商品
     * @param  goodsId 商品ID
     */
    fun collectGoods(goodsId: Int, collect: Boolean) {
        if (collect) {
            val observable = memberApi.addCollection(goodsId)
            baseSubscribe(observable, Consumer { data ->
//                providerView().collect(collect)
                collectGoodsLiveData.value=collect
            })
        } else {
            val observable = memberApi.deleteCollection(goodsId)
            baseSubscribe(observable, Consumer { data ->
//                providerView().collect(collect)
                collectGoodsLiveData.value=collect
            })
        }
    }

    /**
     * @Note   分享商品
     * @param  activity    调用Activity
     * @param  url         分享URL
     * @param  image       商品图片
     * @param  title       标题
     * @param  description 商品标题
     */
    fun shareGoods(activity: Activity, shareBean: ShareBean) {
        val disposable = Observable.just(shareBean.shareImage).compose(ThreadFromUtils.defaultSchedulers()).subscribe({ image ->
            UmengShare.Init(activity.weak().get())
                    .web(shareBean.shareUrl)
                    .setWebTitle(shareBean.shareTitle)
                    .setWebImage(image)
                    .setWebDescription(shareBean.description)
                    .webShare()
        }, {
           showMessage(R.string.分享失败)
        })
        compositeDisposable.add(disposable)
    }
}